import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride_rex/constant.dart';
import 'package:ride_rex/net/common_global_url.dart';
import 'package:ride_rex/net/common_net.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class ApiMine extends CommonApi {
  ApiMine.info({required String userId}) : super(CommonGlobalUrl.userInfo, params: {'userId': userId});
}

///用户服务
class UserService extends GetxService {
  static UserService get instance => Get.find<UserService>();

  final ValueNotifier<MineInfoBean> _mineInfoBean = ValueNotifier(MineInfoBean());

  String get userId => _mineInfoBean.value.userId.getAuto(CommonStorage().getString(StorageKey.spKeyUserId));

  Completer? _finishGetInfo;

  ///信息展示使用
  ValueNotifier<MineInfoBean> get notifierOfMineInfo => _mineInfoBean;

  ///保存用户信息
  void setUserInfo(dynamic json) async {
    if (json is! Map) return;
    _mineInfoBean.value = MineInfoBean.fromJson(json);
  }

  /// 获取用户信息
  Future<MineInfoBean> getCacheMineInfoBean([bool refresh = false]) async {
    if (_mineInfoBean.value.isEmptyBean || refresh) _mineInfoBean.value = await _getInfo();
    return _mineInfoBean.value;
  }

  ///获取最新的数据
  Future<MineInfoBean> _getInfo() async {
    String userId = CommonStorage().getString(StorageKey.spKeyUserId);
    if (userId.empty) return MineInfoBean();
    if (_finishGetInfo == null) {
      _finishGetInfo = Completer();
      _finishGetInfo!.complete(() async {
        try {
          dynamic json = await CommonNet.get(ApiMine.info(userId: userId));
          return MineInfoBean.fromJson(json);
        } catch (e) {
          return null;
        }
      }());
    }
    var temp = await _finishGetInfo!.future;
    _finishGetInfo = null;
    if (temp == null) throw Exception();
    return temp;
  }
}

class MineInfoBean {
  String? userId;
  int? userType;
  String? nickname;
  bool? isInternal;
  String? avatar;
  String? avatarUrl;
  String? avatarThumbUrl;
  String? avatarMiddleThumbUrl;
  List<dynamic>? mediumList;
  List<AvatarRespList>? avatarRespList;
  int? gender;
  String? birthday;
  int? age;
  String? country;
  String? pkgName;
  List<dynamic>? mediaList;
  bool? isAnswer;
  int? availableCoins;
  int? auditStatus;
  bool? isShowReviewSupplementTips;
  List<String>? tagsList;
  List<TagDetails>? tagDetails;
  String? rongcloudToken;
  String? rcToken;
  bool? isRecharge;
  bool? isVip;
  int? level;
  int? followNum;
  int? praiseNum;
  bool? isBlock;
  bool? isSwitchNotDisturbIm;
  bool? isSwitchNotDisturbCall;
  bool? isHavePassword;
  bool? isReview;
  bool? isMultiple;
  String? registerPkgName;
  String? registerCountry;
  String? loginPkgName;
  int? giftWallAction;
  bool? hasEquity;
  String? createTime;

  bool get isEmptyBean => userId.empty;

  MineInfoBean(
      {this.userId,
      this.userType,
      this.nickname,
      this.isInternal,
      this.avatar,
      this.avatarUrl,
      this.avatarThumbUrl,
      this.avatarMiddleThumbUrl,
      this.mediumList,
      this.avatarRespList,
      this.gender,
      this.birthday,
      this.age,
      this.country,
      this.pkgName,
      this.mediaList,
      this.isAnswer,
      this.availableCoins,
      this.auditStatus,
      this.isShowReviewSupplementTips,
      this.tagsList,
      this.tagDetails,
      this.rongcloudToken,
      this.rcToken,
      this.isRecharge,
      this.isVip,
      this.level,
      this.followNum,
      this.praiseNum,
      this.isBlock,
      this.isSwitchNotDisturbIm,
      this.isSwitchNotDisturbCall,
      this.isHavePassword,
      this.isReview,
      this.isMultiple,
      this.registerPkgName,
      this.registerCountry,
      this.loginPkgName,
      this.giftWallAction,
      this.hasEquity,
      this.createTime});

  MineInfoBean.fromJson(dynamic json) {
    if (json["userId"] is String) {
      userId = json["userId"];
    }
    if (json["userType"] is num) {
      userType = (json["userType"] as num).toInt();
    }
    if (json["nickname"] is String) {
      nickname = json["nickname"];
    }
    if (json["isInternal"] is bool) {
      isInternal = json["isInternal"];
    }
    if (json["avatar"] is String) {
      avatar = json["avatar"];
    }
    if (json["avatarUrl"] is String) {
      avatarUrl = json["avatarUrl"];
    }
    if (json["avatarThumbUrl"] is String) {
      avatarThumbUrl = json["avatarThumbUrl"];
    }
    if (json["avatarMiddleThumbUrl"] is String) {
      avatarMiddleThumbUrl = json["avatarMiddleThumbUrl"];
    }
    if (json["mediumList"] is List) {
      mediumList = json["mediumList"] ?? [];
    }
    if (json["avatarRespList"] is List) {
      avatarRespList = json["avatarRespList"] == null
          ? null
          : (json["avatarRespList"] as List).map((e) => AvatarRespList.fromJson(e)).toList();
    }
    if (json["gender"] is num) {
      gender = (json["gender"] as num).toInt();
    }
    if (json["birthday"] is String) {
      birthday = json["birthday"];
    }
    if (json["age"] is num) {
      age = (json["age"] as num).toInt();
    }
    if (json["country"] is String) {
      country = json["country"];
    }
    if (json["pkgName"] is String) {
      pkgName = json["pkgName"];
    }
    if (json["mediaList"] is List) {
      mediaList = json["mediaList"] ?? [];
    }
    if (json["isAnswer"] is bool) {
      isAnswer = json["isAnswer"];
    }
    if (json["availableCoins"] is num) {
      availableCoins = (json["availableCoins"] as num).toInt();
    }
    if (json["auditStatus"] is num) {
      auditStatus = (json["auditStatus"] as num).toInt();
    }
    if (json["isShowReviewSupplementTips"] is bool) {
      isShowReviewSupplementTips = json["isShowReviewSupplementTips"];
    }
    if (json["tagsList"] is List) {
      tagsList = json["tagsList"] == null ? null : List<String>.from(json["tagsList"]);
    }
    if (json["tagDetails"] is List) {
      tagDetails =
          json["tagDetails"] == null ? null : (json["tagDetails"] as List).map((e) => TagDetails.fromJson(e)).toList();
    }
    if (json["rongcloudToken"] is String) {
      rongcloudToken = json["rongcloudToken"];
    }
    if (json["rcToken"] is String) {
      rcToken = json["rcToken"];
    }
    if (json["isRecharge"] is bool) {
      isRecharge = json["isRecharge"];
    }
    if (json["isVip"] is bool) {
      isVip = json["isVip"];
    }
    if (json["level"] is num) {
      level = (json["level"] as num).toInt();
    }
    if (json["followNum"] is num) {
      followNum = (json["followNum"] as num).toInt();
    }
    if (json["praiseNum"] is num) {
      praiseNum = (json["praiseNum"] as num).toInt();
    }
    if (json["isBlock"] is bool) {
      isBlock = json["isBlock"];
    }
    if (json["isSwitchNotDisturbIm"] is bool) {
      isSwitchNotDisturbIm = json["isSwitchNotDisturbIm"];
    }
    if (json["isSwitchNotDisturbCall"] is bool) {
      isSwitchNotDisturbCall = json["isSwitchNotDisturbCall"];
    }
    if (json["isHavePassword"] is bool) {
      isHavePassword = json["isHavePassword"];
    }
    if (json["isReview"] is bool) {
      isReview = json["isReview"];
    }
    if (json["isMultiple"] is bool) {
      isMultiple = json["isMultiple"];
    }
    if (json["registerPkgName"] is String) {
      registerPkgName = json["registerPkgName"];
    }
    if (json["registerCountry"] is String) {
      registerCountry = json["registerCountry"];
    }
    if (json["loginPkgName"] is String) {
      loginPkgName = json["loginPkgName"];
    }
    if (json["giftWallAction"] is num) {
      giftWallAction = (json["giftWallAction"] as num).toInt();
    }
    if (json["hasEquity"] is bool) {
      hasEquity = json["hasEquity"];
    }
    if (json["createTime"] is String) {
      createTime = json["createTime"];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["userId"] = userId;
    _data["userType"] = userType;
    _data["nickname"] = nickname;
    _data["isInternal"] = isInternal;
    _data["avatar"] = avatar;
    _data["avatarUrl"] = avatarUrl;
    _data["avatarThumbUrl"] = avatarThumbUrl;
    _data["avatarMiddleThumbUrl"] = avatarMiddleThumbUrl;
    if (mediumList != null) {
      _data["mediumList"] = mediumList;
    }
    if (avatarRespList != null) {
      _data["avatarRespList"] = avatarRespList?.map((e) => e.toJson()).toList();
    }
    _data["gender"] = gender;
    _data["birthday"] = birthday;
    _data["age"] = age;
    _data["country"] = country;
    _data["pkgName"] = pkgName;
    if (mediaList != null) {
      _data["mediaList"] = mediaList;
    }
    _data["isAnswer"] = isAnswer;
    _data["availableCoins"] = availableCoins;
    _data["auditStatus"] = auditStatus;
    _data["isShowReviewSupplementTips"] = isShowReviewSupplementTips;
    if (tagsList != null) {
      _data["tagsList"] = tagsList;
    }
    if (tagDetails != null) {
      _data["tagDetails"] = tagDetails?.map((e) => e.toJson()).toList();
    }
    _data["rongcloudToken"] = rongcloudToken;
    _data["rcToken"] = rcToken;
    _data["isRecharge"] = isRecharge;
    _data["isVip"] = isVip;
    _data["level"] = level;
    _data["followNum"] = followNum;
    _data["praiseNum"] = praiseNum;
    _data["isBlock"] = isBlock;
    _data["isSwitchNotDisturbIm"] = isSwitchNotDisturbIm;
    _data["isSwitchNotDisturbCall"] = isSwitchNotDisturbCall;
    _data["isHavePassword"] = isHavePassword;
    _data["isReview"] = isReview;
    _data["isMultiple"] = isMultiple;
    _data["registerPkgName"] = registerPkgName;
    _data["registerCountry"] = registerCountry;
    _data["loginPkgName"] = loginPkgName;
    _data["giftWallAction"] = giftWallAction;
    _data["hasEquity"] = hasEquity;
    _data["createTime"] = createTime;
    return _data;
  }

  MineInfoBean copyWith({
    String? userId,
    int? userType,
    String? nickname,
    bool? isInternal,
    String? avatar,
    String? avatarUrl,
    String? avatarThumbUrl,
    String? avatarMiddleThumbUrl,
    List<dynamic>? mediumList,
    List<AvatarRespList>? avatarRespList,
    int? gender,
    String? birthday,
    int? age,
    String? country,
    String? pkgName,
    List<dynamic>? mediaList,
    bool? isAnswer,
    int? availableCoins,
    int? auditStatus,
    bool? isShowReviewSupplementTips,
    List<String>? tagsList,
    List<TagDetails>? tagDetails,
    String? rongcloudToken,
    String? rcToken,
    bool? isRecharge,
    bool? isVip,
    int? level,
    int? followNum,
    int? praiseNum,
    bool? isBlock,
    bool? isSwitchNotDisturbIm,
    bool? isSwitchNotDisturbCall,
    bool? isHavePassword,
    bool? isReview,
    bool? isMultiple,
    String? registerPkgName,
    String? registerCountry,
    String? loginPkgName,
    int? giftWallAction,
    bool? hasEquity,
    String? createTime,
  }) =>
      MineInfoBean(
        userId: userId ?? this.userId,
        userType: userType ?? this.userType,
        nickname: nickname ?? this.nickname,
        isInternal: isInternal ?? this.isInternal,
        avatar: avatar ?? this.avatar,
        avatarUrl: avatarUrl ?? this.avatarUrl,
        avatarThumbUrl: avatarThumbUrl ?? this.avatarThumbUrl,
        avatarMiddleThumbUrl: avatarMiddleThumbUrl ?? this.avatarMiddleThumbUrl,
        mediumList: mediumList ?? this.mediumList,
        avatarRespList: avatarRespList ?? this.avatarRespList,
        gender: gender ?? this.gender,
        birthday: birthday ?? this.birthday,
        age: age ?? this.age,
        country: country ?? this.country,
        pkgName: pkgName ?? this.pkgName,
        mediaList: mediaList ?? this.mediaList,
        isAnswer: isAnswer ?? this.isAnswer,
        availableCoins: availableCoins ?? this.availableCoins,
        auditStatus: auditStatus ?? this.auditStatus,
        isShowReviewSupplementTips: isShowReviewSupplementTips ?? this.isShowReviewSupplementTips,
        tagsList: tagsList ?? this.tagsList,
        tagDetails: tagDetails ?? this.tagDetails,
        rongcloudToken: rongcloudToken ?? this.rongcloudToken,
        rcToken: rcToken ?? this.rcToken,
        isRecharge: isRecharge ?? this.isRecharge,
        isVip: isVip ?? this.isVip,
        level: level ?? this.level,
        followNum: followNum ?? this.followNum,
        praiseNum: praiseNum ?? this.praiseNum,
        isBlock: isBlock ?? this.isBlock,
        isSwitchNotDisturbIm: isSwitchNotDisturbIm ?? this.isSwitchNotDisturbIm,
        isSwitchNotDisturbCall: isSwitchNotDisturbCall ?? this.isSwitchNotDisturbCall,
        isHavePassword: isHavePassword ?? this.isHavePassword,
        isReview: isReview ?? this.isReview,
        isMultiple: isMultiple ?? this.isMultiple,
        registerPkgName: registerPkgName ?? this.registerPkgName,
        registerCountry: registerCountry ?? this.registerCountry,
        loginPkgName: loginPkgName ?? this.loginPkgName,
        giftWallAction: giftWallAction ?? this.giftWallAction,
        hasEquity: hasEquity ?? this.hasEquity,
        createTime: createTime ?? this.createTime,
      );
}

class TagDetails {
  String? tag;
  String? tagTip;
  String? tagColor;

  TagDetails({this.tag, this.tagTip, this.tagColor});

  TagDetails.fromJson(Map<String, dynamic> json) {
    if (json["tag"] is String) {
      tag = json["tag"];
    }
    if (json["tagTip"] is String) {
      tagTip = json["tagTip"];
    }
    if (json["tagColor"] is String) {
      tagColor = json["tagColor"];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["tag"] = tag;
    _data["tagTip"] = tagTip;
    _data["tagColor"] = tagColor;
    return _data;
  }

  TagDetails copyWith({
    String? tag,
    String? tagTip,
    String? tagColor,
  }) =>
      TagDetails(
        tag: tag ?? this.tag,
        tagTip: tagTip ?? this.tagTip,
        tagColor: tagColor ?? this.tagColor,
      );
}

class AvatarRespList {
  String? mediaPath;
  String? mediaUrl;
  String? middleThumbUrl;
  String? thumbUrl;

  AvatarRespList({this.mediaPath, this.mediaUrl, this.middleThumbUrl, this.thumbUrl});

  AvatarRespList.fromJson(Map<String, dynamic> json) {
    if (json["mediaPath"] is String) {
      mediaPath = json["mediaPath"];
    }
    if (json["mediaUrl"] is String) {
      mediaUrl = json["mediaUrl"];
    }
    if (json["middleThumbUrl"] is String) {
      middleThumbUrl = json["middleThumbUrl"];
    }
    if (json["thumbUrl"] is String) {
      thumbUrl = json["thumbUrl"];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["mediaPath"] = mediaPath;
    _data["mediaUrl"] = mediaUrl;
    _data["middleThumbUrl"] = middleThumbUrl;
    _data["thumbUrl"] = thumbUrl;
    return _data;
  }

  AvatarRespList copyWith({
    String? mediaPath,
    String? mediaUrl,
    String? middleThumbUrl,
    String? thumbUrl,
  }) =>
      AvatarRespList(
        mediaPath: mediaPath ?? this.mediaPath,
        mediaUrl: mediaUrl ?? this.mediaUrl,
        middleThumbUrl: middleThumbUrl ?? this.middleThumbUrl,
        thumbUrl: thumbUrl ?? this.thumbUrl,
      );
}
